package Game.Effects 
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import Common.Managers.SwfManager;
	/**
	 * ...
	 * @author Yevgeniy Logachev
	 */
	public class Explosion extends MovieClip
	{
		private static const 	m_animSpeed:int 		= 50;
		private var 			m_explosion:MovieClip	= null;
		private var 			m_animTimer:Timer		= null;
		public static var 		m_swfPath:String 		= Settings.pathRes + "swf/Explosion.swf";
		private static var 		m_explosionType:Array 	= ["Explosion1", "Explosion1", "Explosion1", "Explosion1"];
				
		public function Explosion(startX:int, startY:int) 
		{
			super();
		
			SwfManager.instance.load(m_swfPath, onLoadExplosion);
			
			function onLoadExplosion():void
			{
				var ExplosionClass:Class = SwfManager.instance.getSWF(m_swfPath, m_explosionType[0]);
				m_explosion = new ExplosionClass();
				m_explosion.gotoAndStop(0);
				m_explosion.x = startX;
				m_explosion.y = startY;
				m_explosion.name = "explosion";
				Main.mainSprite.addChild(m_explosion);
				
				m_animTimer = new Timer(m_animSpeed, 23);
				m_animTimer.addEventListener(TimerEvent.TIMER, update, false, 0, true);
				m_animTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onComplete);
				m_animTimer.start();
			}
		}
		
		public static function loadResource(funcHandler:Function):void
		{
			SwfManager.instance.load(m_swfPath, funcHandler);
		}
		
		public static function isLoaded():Boolean
		{
			return SwfManager.instance.isLoaded(m_swfPath);
		}
		
		public function update(e:TimerEvent):void
		{
			m_explosion.gotoAndStop(m_explosion.currentFrame + 1);
		}
		
		public function onComplete(e:TimerEvent):void
		{
			m_animTimer.removeEventListener(TimerEvent.TIMER, onComplete);
			
			Main.mainSprite.removeChild(m_explosion);
			m_explosion = null;
		}
	}
}